#include <iostream>
namespace lzb
{
	template<class T>
	class shared_ptr {
	private:
		T* _ptr;
		int* _cnt;
	public:
		shared_ptr(T* ptr)
			:_ptr(ptr)
			,_cnt(new int(1))
		{}
		~shared_ptr()
		{
			if (--(*_cnt) == 0)
			{
				delete _ptr;
				delete _cnt;
				_ptr = nullptr;
			}
		}
		shared_ptr(const shared_ptr& other)
		{

			_ptr = other._ptr;
			_cnt = other._cnt;
			++(*_cnt);

		}

		shared_ptr& operator = (const shared_ptr& other)
		{
			if (*this != other)
			{
				if (_ptr != nullptr && --(*_cnt) == 0)
				{
					delete _ptr;
					delete _cnt;
				}
				_ptr = other._ptr;
				_cnt = other._cnt;
				++(*_cnt);

			}
			return *this;
		}

		T operator * ()
		{
			return *_ptr;
		}

		int get_cnt()
		{
			return *_cnt;
		}
	};
}


int main()
{
	lzb::shared_ptr<int> sp1(new int(1));
	std::cout << sp1.get_cnt() << std::endl;
	{
		lzb::shared_ptr<int> sp2(sp1);
		std::cout << sp2.get_cnt() << std::endl;
	}
	std::cout << sp1.get_cnt() << std::endl;
	return 0;
}